package com.culturesalon.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.culturesalon.dao.CategoryDAO;
import com.culturesalon.dao.CategoryDAOImpl;
import com.culturesalon.dao.GoodsDAO;
import com.culturesalon.dao.GoodsDAOImpl;
import com.culturesalon.dao.NoticeDAO;
import com.culturesalon.dao.NoticeDAOImpl;
import com.culturesalon.dto.Category;
import com.culturesalon.dto.Goods;
import com.culturesalon.dto.Notice;
import com.culturesalon.dto.User;

/**
 * Servlet implementation class HomePage
 */
@WebServlet("/homepage.do")
public class HomePage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomePage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String redirectjsp=request.getParameter("redirectjsp");
		
		GoodsDAO goodsDAO=new GoodsDAOImpl();
		CategoryDAO categoryDAO=new CategoryDAOImpl();
		List<Goods> recommendGoodsList=new ArrayList<Goods>();
		List<Goods> hotGoodsList=new ArrayList<Goods>();
		
		//获得各类通知的列表
		List<Notice> noticeList1=new ArrayList<Notice>();
		List<Notice> noticeList2=new ArrayList<Notice>();
		List<Notice> noticeList3=new ArrayList<Notice>();
		List<Notice> noticeList4=new ArrayList<Notice>();
		NoticeDAO noticeDAO=new NoticeDAOImpl();
		noticeList1=noticeDAO.findAllById("1", 9);//传统节日介绍
		noticeList2=noticeDAO.findAllById("2", 9);//沙龙历史介绍
		noticeList3=noticeDAO.findAllById("3", 9);//勤工助学信息
		noticeList4=noticeDAO.findAllById("4", 8);//沙龙通知公告
		
		recommendGoodsList=goodsDAO.findRecommend(4); //获得4个推荐产品
		hotGoodsList=goodsDAO.findHot(4);//获得4个热销产品
		List<Goods> goodsList=new ArrayList<Goods>();
		List<Category> topCategoryList=new ArrayList<Category>();
		topCategoryList=categoryDAO.findTop();
		Map<String,List<Goods>> goodsMap = new HashMap<String,List<Goods>>();
		for (Category category:topCategoryList){
			List<Category> subCategoryList=new ArrayList<Category>();
			subCategoryList=categoryDAO.findAllByTree(subCategoryList, "--", category.getId());
			subCategoryList.add(category);
			goodsList=goodsDAO.findAllByCategoryAndSub(subCategoryList, 8);
			goodsMap.put("CategoryAndSub"+category.getId(), goodsList);
		}
		
		request.setAttribute("noticeList1", noticeList1);
		request.setAttribute("noticeList2", noticeList2);
		request.setAttribute("noticeList3", noticeList3);
		request.setAttribute("noticeList4", noticeList4);
		request.setAttribute("topcategorylist",topCategoryList);
		request.setAttribute("goodsMap", goodsMap);
		request.setAttribute("recommendGoodsList", recommendGoodsList);
		request.setAttribute("hotGoodsList", hotGoodsList);
		User user=(User) request.getSession().getAttribute("user");

			
			if(user!=null){
				request.getRequestDispatcher("index_notice1.jsp").forward(request, response);
			}
			else{
				request.getRequestDispatcher("index_notice2.jsp").forward(request, response);
				
			}
		
	}
}
